package com.example.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewTest extends AppCompatActivity {
    private RecyclerView recyclerView;
    private UserAdapter adapter;
    private List<User> userList = new ArrayList<>();

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recycler_view);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.recyclerViewTest), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initData();
        initRecyclerView();
    }

    private void initData() {
        // 添加模拟数据
        userList.add(new User("张三", "zhangsan@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("李四", "lisi@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("王五", "wangwu@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("赵六", "zhaoliu@email.com", R.drawable.ic_launcher_foreground));
        userList.add(new User("钱七", "qianqi@email.com", R.drawable.ic_launcher_foreground));

        // 可以继续添加更多数据...
        for (int i = 6; i <= 20; i++) {
            userList.add(new User("用户" + i, "user" + i + "@email.com", R.mipmap.ic_launcher));
        }
    }

    private void initRecyclerView() {
        recyclerView = findViewById(R.id.recyclerView);
        Log.d("errerrerr", String.valueOf(recyclerView));
        // 设置布局管理器
        try {
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
        } catch (Exception e) {
            Log.d("errerrerr", "initRecyclerView: ");
        }


        // 创建适配器并设置
        adapter = new UserAdapter(userList, new UserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                User user = userList.get(position);
                Toast.makeText(RecyclerViewTest.this, "点击: " + user.getName(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(int position) {
                User user = userList.get(position);
                Toast.makeText(RecyclerViewTest.this, "长按: " + user.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        recyclerView.setAdapter(adapter);
    }
}